package com.stefanini.pe.bbva.bp.in.bo.datasource;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRField;

public class SubRepPerfilamientoDS implements JRDataSource{
	
	private List lista = new ArrayList();
    private int index= -1;
	
    public SubRepPerfilamientoDS(List lista){
    	this.lista=lista;
    }
    
	public Object getFieldValue(JRField field) throws JRException {
        Object value = null;
        String fieldName = field.getName();
        HashMap object = (HashMap)lista.get(index);
        
        if ("F_DESCRIPCION".equals(fieldName)) {
            value = object.get("nombre");
        }else if ("F_PESO".equals(fieldName)) {
        	 value = (object.get("peso")!=null?new BigDecimal(object.get("peso").toString()):new BigDecimal(0));
        }
        else if ("F_PESO_D".equals(fieldName)) {
       	 value = (object.get("pesod")!=null?object.get("pesod").toString():"");
        }
        else if ("F_DESCRIPCION_LABEL".equals(fieldName)) {
        	String[] desc=null;
        	String descLabel="";
        	String peso=object.get("pesod")!=null?object.get("pesod").toString():"";
        	if(object.get("nombre")!=null){
        		desc = object.get("nombre").toString().trim().split(" ");
        	}
        	
        	if(desc!=null){
        		for(int i=0;i<desc.length;i++){
        			descLabel = descLabel.concat(desc[i].concat("\n"));
        		}
        	}
            value = descLabel.concat(peso);
        }
        return value;
	}

	public boolean next() throws JRException {
		index++;
        return (index < lista.size());
	}
}
